<?php

namespace app\admin\controller;

use app\admin\service\AdminService;
use think\exception\ValidateException;
use think\facade\View;

class LoginController
{

    public function form()
    {   
        return View::fetch();
    }

    public function check()
    {   
        $param = request()->post();

        try{
            validate([
                'username' => 'require',
                'password' => 'require'
            ],[
                'username.require' => '用户名不能为空',
                'password.require' => '密码不能为空'
            ])->check($param);

        } catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);   
        }
    

        $adminService = new AdminService();
        $result = $adminService->checkUser($param['username'],$param['password']);

        return $result ? json(['code' => 1, 'msg' => '登录成功!']) : json(['code' => 0, 'msg' => '用户名或密码不正确!']);
    
    }
}